#
# if you want the ram-disk device, define this to be the
# size in blocks.
#
include Makefile.defs

RAMDISK =  -DRAMDISK=512

#
# ROOT_DEV specifies the default root-device when making the image.
# This can be either FLOPPY, /dev/xxxx or empty, in which case the
# default of /dev/hd6 is used by 'build'.
# 
# 指定根文件系统是否在软盘，这里没有用到。因为目标的配置用到的是硬盘
ROOT_DEV= #FLOPPY 

ARCHIVES=kernel/kernel.o mm/mm.o fs/fs.o
DRIVERS =kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a
MATH	=kernel/math/math.a
LIBS	=lib/lib.a

.c.s:
	@$(CC) $(CFLAGS) -S -o $*.s $<
.s.o:
	@$(AS)  -o $*.o $<
.c.o:
	@$(CC) $(CFLAGS) -c -o $*.o $<

all:	Image

# 生成bootsect, setup, system, build。然后利用build工具将前3者合并为Image文件
# boot/setup为Minix文件格式
# 缩小system的体积，只保留关键性的代码和数据，生成的文件名称改为kernel
# 然后将boot、sectup、kernel以及根文件系统设备，写入Image中
# 删除临时生成的kernel文件
Image: boot/bootsect boot/setup tools/system tools/build
	tools/build boot/bootsect boot/setup tools/system $(ROOT_DEV)
	rm -f tools/kernel
	sync

# 往物理机器上的软盘写Image，由于我们这里没有软盘，用的虚拟机，所以这个东西不用
disk: Image
	dd bs=8192 if=Image of=/dev/fd0

# 生成build工具
tools/build: tools/build.c
	$(HOST_GCC) -o tools/build tools/build.c

boot/head.o: boot/head.s
	$(AS) -I./include -o head.o boot/head.s
	mv head.o boot/

# 生成操作系统内核文件，其依赖于各种库、目标文件等
tools/system:	boot/head.o init/main.o \
		$(ARCHIVES) $(DRIVERS) $(MATH) $(LIBS)
	$(LD) $(LDFLAGS) boot/head.o init/main.o \
	$(ARCHIVES) \
	$(DRIVERS) \
	$(MATH) \
	$(LIBS) \
	-o tools/system.elf 
	$(OBJCOPY) -O binary -R .note -R .comment tools/system.elf tools/system
	# nm tools/system | grep -v '\(compiled\)\|\(\.o$$\)\|\( [aU] \)\|\(\.\.ng$$\)\|\(LASH[RL]DI\)'| sort > System.map 

# 操作系统内核生成所需要的各种.a。o生成的规则
kernel/math/math.a:
	(cd kernel/math; make)

kernel/blk_drv/blk_drv.a:
	(cd kernel/blk_drv; make)

kernel/chr_drv/chr_drv.a:
	(cd kernel/chr_drv; make)

kernel/kernel.o:
	(cd kernel; make)

mm/mm.o:
	(cd mm; make)

fs/fs.o:
	(cd fs; make)

lib/lib.a:
	(cd lib; make)

# 生成setup模块，只依赖steup.s
boot/setup: boot/setup.s
	$(AS) -o boot/setup.o boot/setup.s
	$(LD) -m elf_i386 -Ttext 0 -o boot/setup.elf boot/setup.o
	${OBJCOPY} -O binary boot/setup.elf boot/setup

# 生成bootsect模块，只依赖bootsect.s
boot/bootsect:	boot/bootsect.s
	$(AS) -o boot/bootsect.o boot/bootsect.s
	$(LD) -m elf_i386  -Ttext 0  -o boot/bootsect.elf boot/bootsect.o
	${OBJCOPY} -O binary boot/bootsect.elf boot/bootsect

# 生成某种反汇编文件，略
tmp.s:	boot/bootsect.s tools/system
	(echo -n "SYSSIZE = (";ls -l tools/system | grep system \
		| cut -c25-31 | tr '\012' ' '; echo "+ 15 ) / 16") > tmp.s
	cat boot/bootsect.s >> tmp.s

# 清理之前生成的临时性的信息
clean:
	rm -f Image System.map tmp_make core boot/bootsect boot/setup
	rm -f init/*.o tools/system tools/build boot/*.o boot/*.elf
	(cd mm;make clean)
	(cd fs;make clean)
	(cd kernel;make clean)
	(cd lib;make clean)

# 当前的所有文件打包生成backup.Z，用于某种备份吧
backup: clean
	(cd .. ; tar cf - linux | compress16 - > backup.Z)
	sync

# 生成main.o的依赖文件，会将当前自己这个文件的下面Dependenceies给捡的成实际的信号头文件
# 同时会将其它模块的这些Makefile文件也进行替换
dep:
	sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
	(for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done) >> tmp_make
	cp tmp_make Makefile
	(cd fs; make dep)
	(cd kernel; make dep)
	(cd mm; make dep)

### Dependencies:
init/main.o: init/main.c include/unistd.h include/sys/stat.h \
  include/sys/types.h include/sys/times.h include/sys/utsname.h \
  include/utime.h include/time.h include/linux/tty.h include/termios.h \
  include/linux/sched.h include/linux/head.h include/linux/fs.h \
  include/linux/mm.h include/signal.h include/asm/system.h \
  include/asm/io.h include/stddef.h include/stdarg.h include/fcntl.h
